package com.uk.ui.fatura;

import com.vaadin.data.validator.IntegerValidator;
import com.vaadin.ui.Button;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.Notification;

public class FaturaSearchView extends Panel {

	private final TextField kontrataTextField = new TextField("Kontrata ID");
	private FaturaPanel faturaPanel;
	
	public FaturaSearchView(FaturaPanel faturaPanel){
		addStyleName("view");
		
		setSizeFull();
		this.faturaPanel = faturaPanel;
		FormLayout formLayout = new FormLayout();
		setContent(formLayout);
	
		this.kontrataTextField.addValidator(new IntegerValidator("ID e kontrates duhet te jete numer"));
		this.kontrataTextField.setImmediate(true);
		Button search = new Button("Kerko");
		
		search.addListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				performSearch();
				
			}
		});
		
		addComponent(kontrataTextField);
		addComponent(search);
	}
	
	private void performSearch(){
		String kontrataId = (String) kontrataTextField.getValue();

		if ((kontrataId == null || "".equals(kontrataId)) ){
			getWindow().showNotification("Duhet te jepni ID e kontrates !", Notification.TYPE_WARNING_MESSAGE);
			return;
		}
		
			Integer id = Integer.parseInt(kontrataId);
		
		
		faturaPanel.searchKontrata(id);
		
	}
	
}
